var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
// 环境变量的配置 dev 和online
console.log(process.env.WEBPACK_ENV);
var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';
// console.log(process);
// console.log('WEBPACK_ENV:',WEBPACK_ENV);
// 获取html-webpack-plugin参数的方法
var getHtmlConfig = function (name, title) {
    return {
        title: title,
        template: './src/tpl/' + name + '.html',
        filename: './' + name + '.html',
        inject: true,
        hash: true,
        // favicon: './favicon.ico',
        minify: {
            // 去除注释
            removeComments: true,
            // 去除空格
            removeEmptyAttributes: true,
            // 去除空属性
            collapseWhitespace: true
        },
        chunks: ['common', name]
    }
}
var baseConfig = {
    // 路径相对于webpack.conf.js
    entry: {
        'src': './src/page/',
        'name': 'index.js'
    }
}
var commonKeyTitle = '纽扣';
var staticResourceRrefix = 'v1';
var assetsSubDirectory = 'static';
// webpack config
var config = {
    entry: {
        'common': [baseConfig.entry.src + 'common/' + baseConfig.entry.name],//全局的css，计算等等
        'index': [baseConfig.entry.src + 'index/' + baseConfig.entry.name],
        'aboutUs': [baseConfig.entry.src + 'aboutUs/' + baseConfig.entry.name],
        'linkUs': [baseConfig.entry.src + 'linkUs/' + baseConfig.entry.name],
        'allProduct': [baseConfig.entry.src + 'allProduct/' + baseConfig.entry.name],
        'product': [baseConfig.entry.src + 'product/' + baseConfig.entry.name],
        'productList': [baseConfig.entry.src + 'productList/' + baseConfig.entry.name],
        'buyNow': [baseConfig.entry.src + 'buyNow/' + baseConfig.entry.name],
        'cart': [baseConfig.entry.src + 'cart/' + baseConfig.entry.name],
        'feedback': [baseConfig.entry.src + 'feedback/' + baseConfig.entry.name],
        'sitemap': [baseConfig.entry.src + 'sitemap/' + baseConfig.entry.name],
    },
    output: {
        // 打包输出路径
        path: path.resolve(__dirname, 'dist'),
        // publicPath: 'dev' === WEBPACK_ENV ? '/' : onlineLink,
        // js,CSS,IMG引用路径，一般常用配置是dist，此处配置./则css路径会这样:域名+publicPath+filename(含路径)
        publicPath: '/',//webpack-dev-server可能会报错。配置了这个可能就好了
        filename: assetsSubDirectory+'/js/'+staticResourceRrefix+'[chunkhash:8].js'
    },
    // 把外部的变量或模块加载进来
    externals: {
        'jquery': 'window.jQuery'
    },
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            },
            {
                test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/,
                loader: 'url-loader?limit=8192&name='+assetsSubDirectory+'/images/'+staticResourceRrefix+'[hash:8].[name].[ext]'
            },
            {
                test: /\.string$/,
                loader: 'html-loader',
                query: {
                    minimize: true,
                    removeAttributeQuotes: false
                }
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                query:
                    {
                        presets: ['es2015']
                    }
            }
        ]
    },
    resolve: {
        alias: {
            node_modules: path.resolve(__dirname, 'node_modules'),
            util: path.resolve(__dirname, 'src/util'),
            page: path.resolve(__dirname, 'src/page'),
            service: path.resolve(__dirname, 'src/service'),
            image: path.resolve(__dirname, 'src/image')
        }
    },
    plugins: [
        // 提取公共通用的模块到js/base.js(公用的一些js代码)
        new webpack.optimize.CommonsChunkPlugin({
            // name: 'commons',
            name: 'common',
            filename: assetsSubDirectory+'/js/'+staticResourceRrefix+'[chunkhash:16].js'
        }),
        // 为了不让css打入js，而是单独打包生成到文件里
        new ExtractTextPlugin(assetsSubDirectory+"/css/"+staticResourceRrefix+"[chunkhash:8].css"),
        // html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index','index-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('aboutUs','aboutUs-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('linkUs','linkUs-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('allProduct','allProduct-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('product','product-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('productList','productList-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('buyNow','buyNow-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('cart','cart-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('feedback','feedback-' + commonKeyTitle)),
        new HtmlWebpackPlugin(getHtmlConfig('sitemap','sitemap-' + commonKeyTitle)),

        new OpenBrowserPlugin({ url: 'http://localhost:8388/index.html' })
    ]
};


if ('dev' === WEBPACK_ENV) {
    config.entry.common.push('webpack-dev-server/client?http://localhost:8388/');
}
module.exports = config;